/******************************************************************************

	COPYRIGHT(C) JONAS 'SORTIE' TERMANSEN 2010.

	This file is part of the Maxsi Library.

	Maxsi Library is free software: you can redistribute it and/or modify it
	under the terms of the GNU Lesser General Public License as published by
	the Free Software Foundation, either version 3 of the License, or (at your
	option) any later version.

	Maxsi Library is distributed in the hope that it will be useful, but
	WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
	or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
	License for more details.

	You should have received a copy of the GNU Lesser General Public License
	along with Maxsi Library. If not, see <http://www.gnu.org/licenses/>.

	Maxsi Library
	A powerful Cross-Platform C++ General Purpose Library that allows you to
	efficiently create high-performance and reliable applications.

	MaxsiError.cpp
	Defines all the error codes used within the Maxsi Library project.

****************************************************************************/

#include "MaxsiLibrary.h"

BeginMaxsiNamespace

MaxsiError MAXSI_THREADED_VARIABLE LastMaxsiError = MAXSI_ERROR_SUCCESS;

MaxsiError GetError()
{
	return LastMaxsiError;
}

MaxsiError GetAnError(MaxsiError Default)
{
	return ( LastMaxsiError != MAXSI_ERROR_SUCCESS ) ? LastMaxsiError : Default;
}

void SetError(MaxsiError Error)
{
	LastMaxsiError = Error;
}

MESTR* MaxsiErrors[] =
{
	_MESTR("MAXSI_ERROR_SUCCESS"),
	_MESTR("MAXSI_ERROR_UNSPECIFIED"),
	_MESTR("MAXSI_ERROR_MEM_ALLOC_FAIL"),
	_MESTR("MAXSI_ERROR_OUT_OF_MEM"),
	_MESTR("MAXSI_ERROR_ACCESS_VIOLATION"),
	_MESTR("MAXSI_ERROR_NOT_INITIALIZED"),
	_MESTR("MAXSI_ERROR_IS_IN_USE"),
	_MESTR("MAXSI_ERROR_NOT_IMPLEMENTED"),
	_MESTR("MAXSI_ERROR_COULD_NOT_OPEN"),
	_MESTR("MAXSI_ERROR_COULD_NOT_CONNECT"),
	_MESTR("MAXSI_ERROR_INVALID_HEADER"),
	_MESTR("MAXSI_ERROR_UNSUPPORTED_VERSION"),
	_MESTR("MAXSI_ERROR_OUTDATED_VERSION"),
	_MESTR("MAXSI_ERROR_INVALID_INPUT"),
	_MESTR("MAXSI_ERROR_THIS_SHOULD_NOT_HAPPEN"),
	_MESTR("MAXSI_ERROR_FILE_TOO_BIG"),
	_MESTR("MAXSI_ERROR_FILE_TOO_BIG_FOR_FS"),
	_MESTR("MAXSI_ERROR_INPUT_TOO_BIG"),
	_MESTR("MAXSI_ERROR_CONNECTION_ISSUE"),
	_MESTR("MAXSI_ERROR_NOT_SUPPORTED"),
	_MESTR("MAXSI_ERROR_PENDING"),
	_MESTR("MAXSI_ERROR_COULD_NOT_CREATE_SOCKET"),
	_MESTR("MAXSI_ERROR_COULD_NOT_BIND_SOCKET"),
	_MESTR("MAXSI_ERROR_COULD_NOT_SET_NON_BLOCK"),
	_MESTR("MAXSI_ERROR_COULD_NOT_LISTEN"),
	_MESTR("MAXSI_ERROR_COULD_NOT_TRANSFER"),
	_MESTR("MAXSI_ERROR_DNS_LOOKUP_FAILED"),
	_MESTR("MAXSI_ERROR_ALREADY_CONNECTED"),
	_MESTR("MAXSI_ERROR_ALREADY_INITIALIZED"),
	_MESTR("MAXSI_ERROR_COULD_NOT_CREATE_THREAD"),
	_MESTR("MAXSI_ERROR_COULD_NOT_CONVERT_TEXT"),
	_MESTR("MAXSI_ERROR_COULD_NOT_CREATE_WINDOW"),
	_MESTR("MAXSI_ERROR_UNHANDLED_EVENT"),
	_MESTR("MAXSI_ERROR_END_OF_STREAM"),
	_MESTR("MAXSI_ERROR_BUGGY_PROGRAMMER"),
	_MESTR("MAXSI_ERROR_RECEIVED_INVALID_INPUT"),
	_MESTR("MAXSI_ERROR_INTERRUPT"),
	_MESTR("MAXSI_ERROR_INPUT_TOO_SMALL"),
	_MESTR("MAXSI_ERROR_CORRUPT_FILESYSTEM"),
	_MESTR("MAXSI_ERROR_FILE_NOT_FOUND"),
	_MESTR("MAXSI_ERROR_TLS_HANDSHAKE_FAILURE"),
	_MESTR("MAXSI_ERROR_INTERFACE_NOT_IMPLEMENTED"),
};

MESTR* MaxsiErrorName(MaxsiError Error)
{
	return MaxsiErrors[Error];
}

#ifdef Maxsi_Support_W32
MaxsiError W32Error2MaxsiError(DWORD W32Error)
{
	if ( W32Error == ERROR_SUCCESS ) { return MAXSI_ERROR_SUCCESS; }
	if ( W32Error == ERROR_FILE_NOT_FOUND ) { return MAXSI_ERROR_COULD_NOT_OPEN; }
	if ( W32Error == ERROR_PATH_NOT_FOUND ) { return MAXSI_ERROR_COULD_NOT_OPEN; }
	if ( W32Error == ERROR_TOO_MANY_OPEN_FILES ) { return MAXSI_ERROR_COULD_NOT_OPEN; }
	if ( W32Error == ERROR_ACCESS_DENIED ) { return MAXSI_ERROR_ACCESS_VIOLATION; }
	if ( W32Error == ERROR_INVALID_HANDLE ) { return MAXSI_ERROR_INVALID_INPUT; }
	if ( W32Error == ERROR_NOT_ENOUGH_MEMORY ) { return MAXSI_ERROR_OUT_OF_MEM; }
	if ( W32Error == ERROR_NOT_SUPPORTED ) { return MAXSI_ERROR_NOT_SUPPORTED; }
	if ( W32Error == ERROR_INSUFFICIENT_BUFFER ) { return MAXSI_ERROR_INPUT_TOO_SMALL; }

	MAXSI_TODO("Implement more error codes here!");

	return MAXSI_ERROR_UNSPECIFIED;
}
#endif

EndMaxsiNamespace

